package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TodayBestService {
    @Autowired
    private RecommedApiService recommedApiService;
    @Value("${tanhua.sso.default.user}")
    private Long defaultUserId;
    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 业务层实现功能:获取今日佳人的信息
     *
     * @return
     */
    public TodayBest todayBest() {
        //获取token校验后的用户信息
        User user = UserThreadLocal.get();
        Long userId = user.getId();
        if (user == null) {
            //token校验失败,返回null
            return null;
        }
        //获取今日佳人信息
        TodayBest todayBest = recommedApiService.getTodayBest(userId);
        if (todayBest == null) {
            //没有今日佳人,设置默认的佳人信息
            todayBest = new TodayBest();
            todayBest.setFateValue(80L);
            todayBest.setId(defaultUserId);
        }
        //获取今日佳人的用户信息
        UserInfo userInfo = userInfoMapper.selectById(todayBest.getId());
        if (userInfo == null) {
            //没有获取今日佳人的用户信息
            return null;
        }
        //设置今日佳人的用户信息
        todayBest.setAvatar(userInfo.getLogo());
        todayBest.setNickname(userInfo.getNickName());
        todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
        todayBest.setAge(userInfo.getAge());
        todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
        //返回今日佳人的信息
        return todayBest;
    }

    /**
     * 业务层实现功能:获取系统推荐的今日佳人信息
     *
     * @param page
     * @param pageSize
     * @param param
     * @return
     */
    public PageResult getAlltTodayBest(Integer page, Integer pageSize, RecommendUserQueryParam param) {
        PageResult pageResult = new PageResult();
        pageResult.setPagesize(pageSize);
        pageResult.setPage(page);
        User user = UserThreadLocal.get();
        Long userId = user.getId();
        if (user == null) {
            //token校验失败
            return null;
        }
        List<RecommendUser> list = recommedApiService.getAllTodayBest(page, pageSize, userId);
        if (CollUtil.isEmpty(list)) {
            //没有找到系统推荐的佳人
            return null;
        }
        ArrayList<TodayBest> todayBests = new ArrayList<>();
        for (RecommendUser recommendUser : list) {
            TodayBest todayBest = new TodayBest();
            double floor = Math.floor(recommendUser.getScore());
            todayBest.setFateValue(Double.valueOf(Double.valueOf(floor)).longValue());
            todayBest.setId(recommendUser.getUserId());
            QueryWrapper<UserInfo> query = new QueryWrapper<>();
            query.eq("user_id", recommendUser.getUserId());
            UserInfo userInfo = userInfoMapper.selectOne(query);
            //设置今日佳人的用户信息
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man" : "woman");
            todayBest.setAge(userInfo.getAge());
            todayBest.setTags(StringUtils.split(userInfo.getTags(), ','));
            todayBests.add(todayBest);
        }
        pageResult.setItems(todayBests);
        return pageResult;
    }
}
